package org.darkforest.ucenter.service.impl;

import org.darkforest.common.util.DateUtil;
import org.darkforest.dao.mapper.OrderLogMapper;
import org.darkforest.dao.mapper.OrderMapper;
import org.darkforest.dao.mapper.PdLogMapper;
import org.darkforest.enums.OrderLogEnum;
import org.darkforest.enums.OrderStatusEnum;
import org.darkforest.model.Member;
import org.darkforest.model.Order;
import org.darkforest.model.OrderLog;
import org.darkforest.model.PdLogSearchBean;
import org.darkforest.ucenter.service.AccountService;
import org.darkforest.ucenter.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * 
 * 项目名称         :bwmall-ucenter
 * 文件名           :AccountServiceImpl.java
 * 开发公司及作者    :陈超
 * 版本号及开发日期  :Rev. 1.00 (2015年2月19日)
 */
@Service
public class AccountServiceImpl implements AccountService {
	@Autowired
	private PdLogMapper pdLogMapper;
	@Autowired
	private OrderMapper orderMapper;
	@Autowired
	@Qualifier("memberServiceImpl")
	private MemberService memberService;
	@Autowired
	private OrderLogMapper orderLogMapper;

	@Override
	public void getPdLogByMember(PdLogSearchBean page) {
		page.setResultList(pdLogMapper.selectByMember(page));
	}

	/**
	 * 使用账户余额支付
	 * 支付成功需回写order表信息
	 * 并返还多余的保证金: 余额支付时返还所有保证金，线上支付时，返还除线上支付的所有部分
	 * @param orderId
	 * @param payMethod
	 * @return
	 */
	@Transactional
	public boolean doPay(Integer orderId,String orderSn, Integer payMethod) throws Exception{
		boolean res = false;			//支付结果
		Order order = orderMapper.selectByPrimaryKey(orderId);
		BigDecimal cash = new BigDecimal(0);
		if(payMethod==1) {
			//全额现金余额支付
			cash = order.getOrderAmount();
//		}else if(payMethod==2){
//			//电子币+余额折扣支付
////			coin = order.getDiscountCoin();
//			cash = order.getDiscountCash().add(order.getShippingFee());
		}
//		}else{
//			throw new Exception("支付方式异常");
//		}
		Member buyer = memberService.findSimpleMemberById(order.getBuyerId());
		Member seller = memberService.findSimpleMemberById(order.getSellerId());

		//对买家扣除可用余额以及
		int upd = memberService.freezeMemberDeposit(orderId,""+orderSn,buyer.getMemberId(),cash);
		//对卖家返还全部的保证金
		memberService.returnDeposit(orderId,""+orderSn,seller.getMemberId(),order.getOrderAmount(), OrderLogEnum.PAYMENT_DEPOSIT);				//退还保证金


		//回写订单数据
		order.setPdAmount(cash);
//		order.setCoinAmount(coin);
		order.setOrderState("" + OrderStatusEnum.WAIT_SHIPMENTS.getValue());
		order.setPaymentTime(DateUtil.unixTimeStampInt().intValue());						//写入支付时间
		orderMapper.updateByPrimaryKeySelective(order);
		OrderLog orderLog = new OrderLog();
		orderLog.setOrderId(orderId);
		orderLog.setLogUser("" + buyer.getMemberId());
		orderLog.setLogRole("1");
		orderLog.setLogOrderstate("" + OrderStatusEnum.WAIT_SHIPMENTS.getValue());
		orderLog.setLogMsg("余额支付成功");
		orderLog.setLogTime(DateUtil.unixTimeStampInt().intValue());
		orderLogMapper.insert(orderLog);

		return upd>0?true:false;
	}
}
